package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.model.QueryCondition;
import com.example.demo.model.QueryResultDO;
import com.example.demo.model.QueryResultVO;
import com.example.demo.service.QueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/query")
public class QueryController {

    @Autowired
    private QueryService queryService;

    @RequestMapping(value="/doQueryResult", produces = "application/json;charset=utf-8")
    public ResponseEntity queryResult(@RequestBody QueryCondition queryCondition){
        QueryResultVO queryResultVO = new QueryResultVO();
        List<QueryResultDO> queryResultDOS = queryService.queryResult(queryCondition.getStartDate(), queryCondition.getEndDate());
        List<JSONObject> headList = new ArrayList<>();
        HashMap<String, JSONObject> dateToObj = new HashMap<>();
        List<String> headNameList = new ArrayList<>();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("dataItem", "rq");
        jsonObject.put("dataName", "日期");
        headList.add(jsonObject);
        headNameList.add("日期");
        List<JSONObject> dataList = new ArrayList<>();
        for (QueryResultDO queryResultDO : queryResultDOS) {
            if(!headNameList.contains(queryResultDO.getNickName())){
                JSONObject headJson = new JSONObject();
                headJson.put("dataItem", queryResultDO.getNickName());
                headJson.put("dataName", queryResultDO.getNickName());
                headList.add(headJson);
                headNameList.add(queryResultDO.getNickName());
            }
            JSONObject dataJson;
            String date = queryResultDO.getDate();
            if(dateToObj.containsKey(date)){
                dataJson = dateToObj.get(date);
            }
            else{
                dataJson = new JSONObject();
                dataJson.put("rq", date);
                dateToObj.put(date, dataJson);
            }
            dataJson.put(queryResultDO.getNickName(), queryResultDO.getNet());
        }

        for (JSONObject ajsonObj: dateToObj.values()) {
            dataList.add(ajsonObj);
        }

        queryResultVO.setHeader(headList);
        queryResultVO.setData(dataList);
        return  ResponseEntity.ok().body(queryResultVO);
    }

    @RequestMapping(value = "/doQueryResult1", produces = "application/json;charset=utf-8")
    public ResponseEntity queryResultTest(){
        QueryResultVO queryResultVO = new QueryResultVO();
        List<QueryResultDO> queryResultDOS = queryService.queryResult("20200601", "20200630");
        List<JSONObject> headList = new ArrayList<>();
        HashMap<String, JSONObject> dateToObj = new HashMap<>();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("dataItem", "rq");
        jsonObject.put("dataName", "日期");
        headList.add(jsonObject);
        List<JSONObject> dataList = new ArrayList<>();
        for (QueryResultDO queryResultDO : queryResultDOS) {
            JSONObject headJson = new JSONObject();
            headJson.put("dataItem", queryResultDO.getNickName());
            headJson.put("dataName", queryResultDO.getNickName());
            headList.add(headJson);

            JSONObject dataJson;
            String date = queryResultDO.getDate();
            if(dateToObj.containsKey(date)){
                dataJson = dateToObj.get(date);
            }
            else{
                dataJson = new JSONObject();
                dataJson.put("rq", date);
                dateToObj.put(date, dataJson);
            }
            dataJson.put(queryResultDO.getNickName(), queryResultDO.getNet());
        }

        for (JSONObject ajsonObj: dateToObj.values()) {
            dataList.add(ajsonObj);
        }

        queryResultVO.setHeader(headList);
        queryResultVO.setData(dataList);
        return  ResponseEntity.ok().body(queryResultVO);
    }
}
